home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 230_01 / prelude.bun < prev    next >
Text File  |  1987-05-25  |  49KB  |  2,201 lines

  1. : To unbundle, sh this file
  2. echo unbundling Makefile 1>&2
  3. cat >Makefile <<'End'
  4. .SUFFIXES : .st .p
  5. PREPATH = /userfs3/abc/budd/newst/prelude
  6. BINDIR = ../bin
  7.  
  8. PARSED = class.p object.p \
  9. string.p larray.p nil.p array.p\
  10. boolean.p true.p false.p block.p symbol.p \
  11. magnitude.p number.p integer.p char.p float.p radian.p point.p random.p \
  12. collection.p bag.p set.p kcollection.p dictionary.p scollection.p interval.p \
  13. list.p acollection.p file.p bytearray.p \
  14. semaphore.p process.p smalltalk.p
  15.  
  16. .st.p:
  17.     $(BINDIR)/parse $(PREPATH)/$*.st >$*.p
  18.  
  19. install: standard
  20.     -make fastsave
  21.  
  22. bundle: *.st Makefile savescript
  23.     bundle Makefile savescript init *.st >../prelude.bundle
  24.  
  25. standard: $(PARSED)
  26.     cat $(PARSED) init >standard
  27.  
  28. newstd: $(PARSED)
  29.     cat $(PARSED) >newstd
  30.  
  31. fastsave: standard
  32.     $(BINDIR)/st -m <savescript
  33.  
  34. clean:
  35.     -rm *.p
  36.  
  37. # the following are libraries that can be included using the -g switch
  38. #    or using the )g directive
  39.  
  40. #  form - simple ascii graphics using the curses routines
  41. form: form.p
  42.     mv form.p form
  43.  
  44. #  pen - line drawing with the plot(3) routines
  45. pen: pen.p
  46.     mv pen.p pen
  47. End
  48. echo unbundling savescript 1>&2
  49. cat >savescript <<'End'
  50. )s stdsave
  51. End
  52. echo unbundling init 1>&2
  53. cat >init <<'End'
  54. smalltalk new
  55. End
  56. echo unbundling acollection.st 1>&2
  57. cat >acollection.st <<'End'
  58. Class ArrayedCollection :SequenceableCollection
  59. | current |
  60. [
  61.        = anArray                       | i |
  62.                 (self size ~= anArray size) ifTrue: [^ false].
  63.                 i <- 0.
  64.                 self do: [:x | (x ~= (anArray at: (i <- i + 1)))
  65.                                 ifTrue: [^ false]].
  66.         ^ true
  67. |
  68.         at: key ifAbsent: exceptionBlock
  69.         ((key <= 0) or: [key > self size])
  70.             ifTrue: [^ exceptionBlock value].
  71.                 ^ self at: key
  72. |
  73.     coerce: aCollection        | temp |
  74.         temp <- self class new: aCollection size.
  75.         temp replaceFrom: 1 to: aCollection size with: aCollection.
  76.         ^ temp
  77. |
  78.        copyFrom: start to: stop                | size temp |
  79.         size <- stop - start + 1.
  80.         temp <- self class new: size.
  81.         temp replaceFrom: 1 to: size with: self startingAt: start.
  82.         ^ temp
  83. |
  84.         currentKey
  85.                 ^ current
  86. |
  87.     deepCopy        | newobj |
  88.         newobj <- self class new: self size.
  89.         (1 to: self size) do:
  90.             [:i | newobj at: i
  91.                 put: (self at: i) copy ].
  92.         ^ newobj
  93. |
  94.        do: aBlock
  95.                 (1 to: self size)
  96.             do: [:i | current <- i.
  97.                 aBlock value: (self at: i)]
  98. |
  99.        first
  100.                 current <- 1.
  101.                 ^ (current <= self size)
  102.             ifTrue: [ self at: current]
  103. |
  104.     firstKey
  105.         ^ 1
  106. |
  107.     lastKey
  108.         ^ self size
  109. |
  110.     next
  111.                 current <- current + 1.
  112.                 ^ (current <= self size)
  113.             ifTrue: [ self at: current]
  114. |
  115.     padTo: length
  116.         ^ (self size < length)
  117.             ifTrue: [ self ,
  118.                 (self class new: (length - self size) ) ]
  119.             ifFalse: [ self ]
  120. |
  121.     shallowCopy        | newobj |
  122.         newobj <- self class new: self size.
  123.         (1 to: self size) do:
  124.             [:i | newobj at: i
  125.                 put: (self at: i) ].
  126.         ^ newobj
  127. ]
  128. End
  129. echo unbundling array.st 1>&2
  130. cat >array.st <<'End'
  131. Class Array :ArrayedCollection
  132. [
  133.     new: aValue
  134.         ^ <NewArray aValue>
  135. |
  136.     at: aNumber
  137.         ( (aNumber < 1) or: [aNumber > <Size self> ] )
  138.             ifTrue: [ self error: 'index error'. ^nil ].
  139.         ^ <At self aNumber >
  140. |
  141.     at: aNumber put: aValue
  142.         ( (aNumber < 1) or: [aNumber > <Size self> ] )
  143.             ifTrue: [ self error: 'index error'. ^nil ].
  144.         <AtPut self aNumber aValue >.
  145.         ^ aValue
  146. |
  147.     grow: newObject
  148.         ^ <Grow self newObject>
  149. |
  150.     printString        | value i |
  151.         value <- ')'.
  152.         i <- <Size self>.
  153.         [i > 0] whileTrue:
  154.             [ value <- <At self i>  printString ,
  155.                     ' ', value.
  156.                     i <- i - 1].
  157.         ^ '#( ' , value
  158. |
  159.     size
  160.         ^ <Size self>
  161. ]
  162. End
  163. echo unbundling bag.st 1>&2
  164. cat >bag.st <<'End'
  165. Class Bag :Collection
  166. | dict count |
  167. [
  168.         new
  169.                 dict <- Dictionary new
  170.  
  171. |       add: newElement
  172.                 dict at: newElement
  173.                      put: (1 + (dict at: newElement ifAbsent: [0]))
  174.  
  175. |       add: newObj withOccurrences: anInteger
  176.                 anInteger timesRepeat: [ self add: newObj ].
  177.                 ^ newObj
  178.  
  179. |       remove: oldElement ifAbsent: exceptionBlock   | i |
  180.                 i <- dict at: oldElement
  181.                           ifAbsent: [ ^ exceptionBlock value].
  182.                 (1 = i) ifTrue:  [dict removeKey: oldElement]
  183.                         ifFalse: [dict at: oldElement put: i - 1 ]
  184.  
  185. |       size
  186.                 ^ dict inject: 0 into: [:x :y | x + y]
  187.  
  188. |       occurrencesOf: anElement
  189.                 ^ dict at: anElement ifAbsent: [0]
  190.  
  191. |       first
  192.         (count <- dict first) isNil ifTrue: [^ nil].
  193.         count <- count - 1.
  194.         ^ dict currentKey
  195.  
  196. |       next
  197.         [count notNil] whileTrue:
  198.            [ (count > 0)
  199.                 ifTrue: [count <- count - 1. ^ dict currentKey]
  200.             ifFalse: [(count <- dict next) isNil
  201.                     ifTrue: [^ nil] ]].
  202.         ^ nil
  203.  
  204. ]
  205. End
  206. echo unbundling block.st 1>&2
  207. cat >block.st <<'End'
  208. "
  209.     Class Block.
  210.  
  211.     Note how whileTrue: and whileFalse: depend upon the parser
  212.     optimizing the loops into control flow, rather than message
  213.     passing.  If this were not the case, whileTrue: would have to
  214.     be implemented using recursion, as follows:
  215.  
  216.     whileTrue: aBlock
  217.         (self value) ifFalse: [^nil].
  218.         aBlock value.
  219.         ^ self whileTrue: aBlock
  220. "
  221. Class Block
  222. [
  223.     newProcess
  224.         ^ <NewProcess  self>
  225. |
  226.     newProcessWith: argumentArray
  227.         ^ <NewProcess  self argumentArray>
  228. |
  229.     fork
  230.         self newProcess resume.
  231.         ^ nil
  232. |
  233.     forkWith: argumentArray
  234.         (self newProcessWith: argumentArray) resume.
  235.         ^ nil
  236. |
  237.     whileTrue
  238.         ^ [self value ] whileTrue: []
  239. |
  240.     whileTrue: aBlock
  241.         ^ [ self value ] whileTrue: [ aBlock value ]
  242. |
  243.     whileFalse
  244.         ^ [ self value ] whileFalse: []
  245. |
  246.     whileFalse: aBlock
  247.         ^ [ self value ] whileFalse: [ aBlock value ]
  248. |
  249.      value
  250.         <BlockExecute  0>
  251. |
  252.     value: a
  253.         <BlockExecute  1>
  254. |
  255.     value: a value: b
  256.         <BlockExecute  2>
  257. |
  258.     value: a value: b value: c
  259.         <BlockExecute  3>
  260. |
  261.     value: a value: b value: c value: d
  262.         <BlockExecute  4>
  263. |
  264.     value: a value: b value: c value: d value: e
  265.         <BlockExecute  5>
  266. ]
  267. End
  268. echo unbundling boolean.st 1>&2
  269. cat >boolean.st <<'End'
  270. Class Boolean
  271. [
  272.         &    aBoolean
  273.         ^ self and: [aBoolean]
  274.  
  275. |       |    aBoolean
  276.         ^ self or: [aBoolean]
  277.  
  278. |       and: aBlock
  279.         ^ self and: [aBlock value]
  280.  
  281. |       or:  aBlock
  282.         ^ self or: [aBlock value]
  283.  
  284. |    eqv: aBoolean
  285.         ^ self == aBoolean
  286.  
  287. |    xor: aBoolean
  288.         ^ self ~~ aBoolean
  289. ]
  290. End
  291. echo unbundling bytearray.st 1>&2
  292. cat >bytearray.st <<'End'
  293. Class ByteArray :ArrayedCollection
  294. [
  295.     new: size
  296.         ^ <NewByteArray size>
  297. |
  298.     at: index
  299.         ^ <ByteArrayAt self index>
  300. |
  301.     at: index put: value
  302.         <ByteArrayAtPut self index value>
  303. |
  304.     printString    | str |
  305.         str <- '#[ '.
  306.         (1 to: self size)
  307.             do: [:i | str <- str , (self at: i) printString , ' '].
  308.         ^ str , ']'
  309. |
  310.     size
  311.         ^ <ByteArraySize self>
  312. ]
  313.  
  314. End
  315. echo unbundling char.st 1>&2
  316. cat >char.st <<'End'
  317. Class Char :Magnitude
  318. [
  319.     == aChar
  320.         ^ <SameTypeOfObject self aChar>
  321.             ifTrue:  [<CharacterEquality self aChar>]
  322.             ifFalse: [false]
  323. |    < aChar
  324.         ^ <SameTypeOfObject self aChar>
  325.             ifTrue:  [<CharacterLessThan self aChar>]
  326.             ifFalse: [self compareError]
  327. |
  328.     = aChar
  329.         ^ <SameTypeOfObject self aChar>
  330.             ifTrue:  [<CharacterEquality self aChar>]
  331.             ifFalse: [self compareError]
  332. |    > aChar
  333.         ^ <SameTypeOfObject self aChar>
  334.             ifTrue:  [<CharacterGreaterThan self aChar>]
  335.             ifFalse: [self compareError]
  336. |
  337.     asciiValue
  338.         ^ <CharacterToInteger self>
  339. |
  340.     asLowercase
  341.         ^ <IsUpper self>
  342.             ifTrue:  [<ChangeCase self>]
  343.             ifFalse: [self]
  344. |
  345.     asUppercase
  346.         ^ <IsLower self>
  347.             ifTrue:  [<ChangeCase self>]
  348.             ifFalse: [self]
  349. |
  350.     asString
  351.         ^ <CharacterToString self>
  352. |
  353.     compareError
  354.         ^ self error: 'char cannot be compared to non char'
  355. |
  356.     digitValue        | i |
  357.         ((i <- <DigitValue self>) isNil)
  358.             ifTrue: [self error: 'digitValue on nondigit char'].
  359.         ^ i
  360. |
  361.     isAlphaNumeric
  362.         ^ <IsAlnum self>
  363. |
  364.     isDigit
  365.         ^ self between: $0 and: $9
  366. |
  367.     isLetter
  368.         ^ self isLowercase or: [self